/*
 * Copyright (c) 2016-2021, Hangzhou Qulian Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "account.h"
#include "ql_type.h"
#include "CuTest.h"

OhErrType time_ret_zero(OhInt64Type *dst) {
    *dst = 0;
    return ACCOUNT_SUCCESS;
}

void account_test(CuTest* tc) {

    AccountSetTimeFunc(time_ret_zero);

    OhByteType priKey[] = "-----BEGIN EC PRIVATE KEY-----\n"
                         "MHQCAQEEIAICAgEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBoAcGBSuBBAAK\n"
                         "oUQDQgAExUK0Fv9I2BvhZR8lVIwG0ZHPowy4GvUZEYMM8uASzJ/3/Xbd0VlMYHFg\n"
                         "qXkEMxUZJABjkW2PNh3vU4lzCsLXRA==\n"
                         "-----END EC PRIVATE KEY-----";
    //create account
    AccountType my_account;
    OhErrType res = AccountInitX509(&my_account, priKey);
    CuAssertIntEquals(tc, ACCOUNT_SUCCESS, res);
    CuAssertStrEquals(tc, "0x469339bdf3d3621bebea36c308de77939424e34e",(char*)my_account.addr);
    // create tx
    TxType tx;
    OhByteType ex[] = "{\"id\":\"0F384\",\"name\":\"tom\",\"class\":\"0808\"}";
    res = TxSendNew(&tx, &my_account, my_account.addr, 0, ex, (OhByteType *)"[\"12345\"]");
    CuAssertIntEquals(tc, ACCOUNT_SUCCESS, res);
    CuAssertStrEquals(tc, "00aef433b91bf339f466f82199293dae8638370688572b1c14678f4e55b5ea55b91a1389ae0eee77c99d790c326190438225470c3d3480ed685291ed5c826e460400",(char*)tx.signature);

    OhByteType *out = calloc(1000, sizeof(OhByteType));
    res = TxPrint(&tx, ORIGIN_MSG,TX_TYPE_SENDTRANSACTION, out, 1000);
    CuAssertIntEquals(tc, ACCOUNT_SUCCESS, res);
    CuAssertStrEquals(tc, "{\"jsonrpc\":\"2.0\",\"method\":\"tx_sendTransaction\",\"params\":[{\"from\":\"0x469339bdf3d3621bebea36c308de77939424e34e\",\"to\":\"0x469339bdf3d3621bebea36c308de77939424e34e\",\"value\":0,\"timesTamp\":0,\"simulate\":false,\"nonce\":1,\"extra\":\"{\\\"id\\\":\\\"0F384\\\",\\\"name\\\":\\\"tom\\\",\\\"class\\\":\\\"0808\\\"}\",\"signature\":\"00aef433b91bf339f466f82199293dae8638370688572b1c14678f4e55b5ea55b91a1389ae0eee77c99d790c326190438225470c3d3480ed685291ed5c826e460400\"}],\"id\":1}",(char*)out);

    free(out);
}

CuSuite* account_test_suite(void)
{
    CuSuite* suite = CuSuiteNew();
    SUITE_ADD_TEST(suite, account_test);
    return suite;
}
